package com.kkb.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.TeamMapper;
import com.kkb.pojo.QueryTeamVo;
import com.kkb.pojo.Team;
import com.kkb.pojo.TeamExample;
import com.kkb.service.TeamService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class TeamServiceImpl implements TeamService {
    @Resource
    private TeamMapper teamMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public PageInfo<Team> queryByPage(Integer pageNum, Integer pageSize, QueryTeamVo vo) {

        TeamExample teamExample = new TeamExample();
        TeamExample.Criteria criteria = teamExample.createCriteria();
        if (vo != null) {
            if (vo.getTeamName() != null && !"".equals(vo.getTeamName().trim())) {
                criteria.andTeamNameLike("%"+vo.getTeamName().trim()+"%");
            }
            if (vo.getChineseName() != null && !"".equals(vo.getChineseName().trim())) {
                criteria.andChineseNameLike("%"+vo.getChineseName().trim()+"%");
            }
            if (vo.getCoach() != null && !"".equals(vo.getCoach().trim())) {
                criteria.andCoachLike("%"+vo.getCoach().trim()+"%");
            }
            if (vo.getCreateFromTime() != null) {
                criteria.andCreateTimeGreaterThanOrEqualTo(vo.getCreateFromTime());
            }
            if (vo.getCreatetoTime() != null) {
                criteria.andCreateTimeLessThanOrEqualTo(vo.getCreatetoTime());
            }
            if (vo.getArea() != null && vo.getArea()!=-1) {
                criteria.andAreaEqualTo(vo.getArea());
            }
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Team> teams = teamMapper.selectByExample(teamExample);
        return new PageInfo<>(teams);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Integer addTeam(Team team){
        return teamMapper.insertSelective(team);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public Team queryById(Integer integer){
        return teamMapper.selectByPrimaryKey(integer);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Integer updateTeam(Team team){
        return teamMapper.updateByPrimaryKeySelective(team);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Integer deleteTeam(Integer teamId){
        Team team = teamMapper.selectByPrimaryKey(teamId);
        team.setIsDel(1);
        return teamMapper.updateByPrimaryKeySelective(team);
    }
}
